package com.client;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import com.alibaba.fastjson.JSON;

/**
 * Created by tancw on 2016/10/11.
 */
public class LocalHttpClient {
	private static final OkHttpClient client = new OkHttpClient();

	public static <T> T execute(Request request, Class<T> t) {
		try {
			Response response = client.newCall(request).execute();
			return JSON.parseObject(response.body().string(), t);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String get(String url) throws IOException {
		Request request = new Request.Builder().url(url).build();
		Response response = client.newCall(request).execute();
		if (response.isSuccessful()) {
			return response.body().string();
		} else {
			throw new IOException("Unexpected code " + response);
		}
	}
}
